package com.bo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bo.dto.CarHornDTO;
import com.bo.dto.CurOutDTO;
import com.bo.dto.ManufactureDTO;
import com.bo.enity.*;
import com.bo.exception.RestUtil;
import com.bo.result.ParameterCodeEnum;
import com.bo.result.R;
import com.bo.result.Result;
import com.bo.result.ResultCodeEnum;
import com.bo.service.CallNumberService;
import com.bo.service.impl.*;
import com.bo.utils.BigDecimalUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sanyi")
@Slf4j
public class HNTController {

    @Resource
    private SP1RecipeRatioServiceImpl sp1RecipeRatioService;
    @Resource
    private SP2RecipeRatioServiceImpl sp2RecipeRatioService;
    @Resource
    private SP1ManufactureServiceImpl sp1ManufactureService;
    @Resource
    private SP2ManufactureServiceImpl sp2ManufactureService;
    @Resource
    private SP1TaskServiceImpl sp1TaskService;
    @Resource
    public SP2TaskServiceImpl sp2TaskService;
    @Resource
    private SP1OutMaskServiceImpl sp1OutMaskService;
    @Resource
    private SP2OutMaskServiceImpl sp2OutMaskService;


    @Resource
    private SP1CurOutServiceImpl sp1CurOutService;
    @Resource
    private SP2CurOutServiceImpl sp2CurOutService;

    @Resource
    private CallNumberService callNumberService;

    @Value("${mdb.path}")
    private String BASE_PATH;


    @RequestMapping("/result")
    public R getResult() {
        ResultCodeEnum resultCodeEnum = ResultCodeEnum.SUCCESS;
        return R.ok();
    }

    @RequestMapping("/exception")
    public R getException() {
        throw new NullPointerException();
    }

    /**
     * 中间件任务，当新增调度时，将中控数据通过该接口写入到access数据库中
     *
     * @param baseSchedulingDetails
     * @return
     */
    @ApiOperation(value = "生产调度明细-中间件任务", notes = "生产调度明细-中间件任务")
    @PostMapping(value = "/schedulingTask")    //中间件任务
    public R schedulingTask(@Validated @RequestBody BaseSchedulingDetails baseSchedulingDetails) throws SQLException {
        log.info("生产调度明细-中间件任务" + baseSchedulingDetails.toString());
        //先保存任务单
        Integer taskID = this.createTask(baseSchedulingDetails);
        //如果有砂浆则需要处理 quantity数据
        if (baseSchedulingDetails.getMortar() != null && baseSchedulingDetails.getMortar().compareTo(BigDecimal.ZERO) > 0) {
            baseSchedulingDetails.setProductionQuantity(BigDecimalUtils.subtract(baseSchedulingDetails.getProductionQuantity(), baseSchedulingDetails.getMortar()));
        }
        //再保存调度数据
        Integer DeliveryID = this.createTransport(baseSchedulingDetails, taskID);
        //保存出货单
        this.createOutMask(baseSchedulingDetails, taskID, DeliveryID);
        //请求车辆喇叭
        callNumberService.CarHorn(baseSchedulingDetails.getWagon(),baseSchedulingDetails.getProduceLine());
        return R.ok();
    }

    /**
     * 叫号
     *
     * @param baseSchedulingDetails
     * @return
     */
    @ApiOperation(value = "生产调度明细-叫号", notes = "生产调度明细-叫号")
    @PostMapping(value = "/callNumber")    //中间件任务
    public R callNumber(@RequestBody BaseSchedulingDetails baseSchedulingDetails) throws SQLException {
        //请求车辆喇叭
        callNumberService.CarHorn(baseSchedulingDetails.getWagon(),baseSchedulingDetails.getProduceLine());
        return R.ok();
    }


    //创建出货单
    private void createOutMask(BaseSchedulingDetails baseSchedulingDetails, Integer taskID, Integer deliveryID) {
        CurOut curOut = new CurOut();
        curOut.setDeliveryId(deliveryID);
        curOut.setEmployeeId(Integer.valueOf(baseSchedulingDetails.getWagon()));
        curOut.setQuantity(baseSchedulingDetails.getProductionQuantity());
        //盘次逻辑 生产量/3
        curOut.setItemNo(getItemNo(baseSchedulingDetails.getProductionQuantity()));
        curOut.setRecipeName(baseSchedulingDetails.getProductName() + baseSchedulingDetails.getOrderNo());
        String ratioCode = baseSchedulingDetails.getProductName() + baseSchedulingDetails.getRatioNo();
        curOut.setMDateTime(new Date());
        if (baseSchedulingDetails.getProduceLine().contains("1")) {
            RecipeRatio byRatioCode = sp1RecipeRatioService.getByRatioCode(ratioCode);
            if (byRatioCode != null) {
                curOut.setRecipeId(byRatioCode.getRecipeID());
            }
            Integer maxId = sp1CurOutService.getMaxId();
            if(maxId !=null && maxId > 0){
                curOut.setOverId(0);
            }else {
                curOut.setOverId(1);
            }
            curOut.setStationId(1);
            //包含砂浆的数据
            if(baseSchedulingDetails.getMortar() != null && baseSchedulingDetails.getMortar().compareTo(BigDecimal.ZERO) > 0) {
                CurOut shajiangCurOut = new CurOut();
                BeanUtil.copyProperties(curOut, shajiangCurOut);
                shajiangCurOut.setBSlurry(1);
                shajiangCurOut.setDeliveryId(deliveryID + 1);
                shajiangCurOut.setQuantity(baseSchedulingDetails.getMortar());
                shajiangCurOut.setOverId(0);
                shajiangCurOut.setRecipeName(baseSchedulingDetails.getProductName() +"S" + baseSchedulingDetails.getRatioNo());
                String shajiangRatioCode = baseSchedulingDetails.getProductName() +"S" + baseSchedulingDetails.getRatioNo();
                //盘次逻辑 生产量/3
                shajiangCurOut.setItemNo(getItemNo(baseSchedulingDetails.getMortar()));
                RecipeRatio rc = sp1RecipeRatioService.getByRatioCode(shajiangRatioCode);
                if (rc != null) {
                    shajiangCurOut.setRecipeId(rc.getRecipeID());
                }
                //只带砂浆的设置batchNo为2
                if(baseSchedulingDetails.getProductionQuantity().compareTo(BigDecimal.ZERO)  == 0) {
                    shajiangCurOut.setBatchNo(2);
                }
                sp1CurOutService.save(shajiangCurOut);
            }
            //补充逻辑，混凝土生产量为0的不生产
            if(baseSchedulingDetails.getProductionQuantity().compareTo(BigDecimal.ZERO) != 0){
                sp1CurOutService.save(curOut);
            }
        } else if (baseSchedulingDetails.getProduceLine().contains("2")) {
            RecipeRatio byRatioCode = sp2RecipeRatioService.getByRatioCode(ratioCode);
            if (byRatioCode != null) {
                curOut.setRecipeId(byRatioCode.getRecipeID());
            }
            Integer maxId = sp2CurOutService.getMaxId();
            if(maxId !=null && maxId > 0){
                curOut.setOverId(0);
            }else {
                curOut.setOverId(1);
            }
            curOut.setStationId(2);
            //包含砂浆的数据
            if(baseSchedulingDetails.getMortar() != null && baseSchedulingDetails.getMortar().compareTo(BigDecimal.ZERO) > 0) {
                CurOut shajiangCurOut = new CurOut();
                BeanUtil.copyProperties(curOut, shajiangCurOut);
                shajiangCurOut.setBSlurry(1);
                shajiangCurOut.setDeliveryId(deliveryID + 1);
                shajiangCurOut.setQuantity(baseSchedulingDetails.getMortar());
                shajiangCurOut.setOverId(0);
                shajiangCurOut.setRecipeName(baseSchedulingDetails.getProductName() +"S" + baseSchedulingDetails.getRatioNo());
                String shajiangRatioCode = baseSchedulingDetails.getProductName() +"S" + baseSchedulingDetails.getRatioNo();

                //盘次逻辑 生产量/3
                shajiangCurOut.setItemNo(getItemNo(baseSchedulingDetails.getMortar()));
                RecipeRatio rc = sp2RecipeRatioService.getByRatioCode(shajiangRatioCode);
                if (rc != null) {
                    shajiangCurOut.setRecipeId(rc.getRecipeID());
                }
                //只带砂浆的设置batchNo为2
                if(baseSchedulingDetails.getProductionQuantity().compareTo(BigDecimal.ZERO)  == 0) {
                    shajiangCurOut.setBatchNo(2);
                }
                sp2CurOutService.save(shajiangCurOut);
            }
            //补充逻辑，混凝土生产量为0的不生产
            if(baseSchedulingDetails.getProductionQuantity().compareTo(BigDecimal.ZERO)  != 0) {
                sp2CurOutService.save(curOut);
            }
        }
    }

    /**
     * 三一水泥配比；中间件任务，当新增配比时，将标准配比数据通过该接口写入到access数据库中
     *
     * @param baseRatioManagementVOJson
     * @return
     * @throws SQLException
     */
    @ApiOperation(value = "标准配比-中间件任务", notes = "标准配比-中间件任务")
    @PostMapping(value = "/ratioTask")    //中间件任务
    public R cementRatioTask(@Validated @RequestBody BaseRatioManagementVO baseRatioManagementVOJson) throws SQLException {
        RecipeRatio recipeRatio = new RecipeRatio();
        List<BaseRatioManagementDetail> baseRatioManagementDetailList = baseRatioManagementVOJson.getBaseRatioManagementDetailList();
        Map<Integer, BigDecimal> numberMap = baseRatioManagementDetailList.stream().
                filter(detail -> detail.getSort() != null).
                collect(Collectors.toMap(BaseRatioManagementDetail::getSort,
                        detail -> detail.getConstNum() == null? BigDecimal.ZERO : detail.getConstNum(),
                        (a, b) -> b));
        String ratioCode = baseRatioManagementVOJson.getRatioCode();
        String produceLine = baseRatioManagementVOJson.getProduceLine();
        //验证配比是否存在
        RecipeRatio check = null;
        if (produceLine.contains("1")) {
            check = sp1RecipeRatioService.getByRatioCode(ratioCode);
        } else if (produceLine.contains("2")) {
            check = sp2RecipeRatioService.getByRatioCode(ratioCode);
        }
        if (check != null) {
            recipeRatio = check;
        }

        //判断ratioCode是否存在S字母
        if (ratioCode.contains("S")) {
            recipeRatio.setRecipeDSL("0");//砂浆的存0
            recipeRatio.setRecipeSN4("0"); //砂浆的存0
            recipeRatio.setRecipeFMH("0"); //砂浆的存0
            recipeRatio.setRecipeWJJ2("0"); //砂浆的存0
        } else {
            recipeRatio.setRecipeDSL(numberMap.get(1).toString());
            recipeRatio.setRecipeSN4(numberMap.get(3).toString());
            recipeRatio.setRecipeFMH(numberMap.get(8).toString());
            recipeRatio.setRecipeWJJ2(numberMap.get(13).toString());
        }
        recipeRatio.setRecipeSN1(numberMap.get(6).toString());
        recipeRatio.setRecipeZSL(numberMap.get(2).toString());
        recipeRatio.setRecipeSZ(numberMap.get(4).toString());
        recipeRatio.setRecipeXSL(numberMap.get(5).toString());
        recipeRatio.setRecipeSN2(numberMap.get(7).toString());
        recipeRatio.setRecipeKF(numberMap.get(9).toString());
        recipeRatio.setRecipeSHUI(numberMap.get(10).toString());
        //recipeRatio.setSewage(numberMap.get(11).toString());//污水找不到字段
        recipeRatio.setRecipeWJJ1(numberMap.get(12).toString());
        recipeRatio.setRecipeFJJ(numberMap.get(14).toString());
        recipeRatio.setTld(baseRatioManagementVOJson.getSlumps());//塌落度
        recipeRatio.setIntensityLevel(baseRatioManagementVOJson.getProductName());//强度等级
        //手动设置3个参数为null
        recipeRatio.setRecipeSN3(null);
        recipeRatio.setRecipeSZ1(null);
        recipeRatio.setRecipeOth2(null);
        //搅拌时间逻辑处理
        String substring = baseRatioManagementVOJson.getProductName().substring(1, 3);
        Integer recipeOth1 = Integer.valueOf(substring);
        if(recipeOth1 <= 25){
            recipeOth1 = 25;
        }
        recipeRatio.setRecipeOth1(recipeOth1.toString()); //搅拌时间
        recipeRatio.setSszdlj("5-31.5");
        recipeRatio.setSljdj("中");
        BigDecimal sum  = numberMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        recipeRatio.setBgmd(sum.toString());
        //recipeRatio.setRecipeCodeERP(baseRatioManagementVOJson.getRatioCode());//配方编码
        recipeRatio.setRecipeName(baseRatioManagementVOJson.getProductName() + baseRatioManagementVOJson.getRatioCode());//配方名称=产品名称+配比编码
        if (produceLine.contains("1")) {
            if(check == null){
                //获取最大的id值
                Integer maxId = sp1RecipeRatioService.getMaxId();
                recipeRatio.setRecipeID(maxId + 1);
                sp1RecipeRatioService.save(recipeRatio);
            }else {
                UpdateWrapper<RecipeRatio> wrapper = new UpdateWrapper<>();
                wrapper.lambda().eq(RecipeRatio::getRecipeName, recipeRatio.getRecipeName());
                sp1RecipeRatioService.update(recipeRatio, wrapper);
            }
        } else if (produceLine.contains("2")) {
            if(check == null){
                //获取最大的id值
                Integer maxId = sp2RecipeRatioService.getMaxId();
                recipeRatio.setRecipeID(maxId + 1);
                sp2RecipeRatioService.save(recipeRatio);
            }else {
                UpdateWrapper<RecipeRatio> wrapper = new UpdateWrapper<>();
                wrapper.lambda().eq(RecipeRatio::getRecipeName, recipeRatio.getRecipeName());
                sp1RecipeRatioService.update(recipeRatio, wrapper);
            }
        }
        return R.ok();
    }

    /**
     * 任务保存
     *
     * @param baseSchedulingDetails
     * @return
     * @throws SQLException
     */
    @ApiOperation(value = "任务保存-中间件任务", notes = "任务保存-中间件任务")
    @PostMapping(value = "/taskSave")    //中间件任务
    public R taskSave(@Validated @RequestBody BaseSchedulingDetails baseSchedulingDetails) throws SQLException {
        this.createTask(baseSchedulingDetails);
        return R.ok();
    }

    //创建任务单
    private Integer createTask(BaseSchedulingDetails baseSchedulingDetails) {
        Task task = new Task();
        //先查询任务单是否存在，如果不存在，则新增
        String produceLine = baseSchedulingDetails.getProduceLine();//生产线
        String recipeNo = baseSchedulingDetails.getProductName() + baseSchedulingDetails.getRatioNo();
        Task check = new Task();
        if (produceLine.contains("1")) {
            check = sp1TaskService.getByRecipeNo(recipeNo);
        } else if (produceLine.contains("2")) {
            check = sp2TaskService.getByRecipeNo(recipeNo);
        }
        if (check == null) {
            task.setProjectName(baseSchedulingDetails.getEngineeringName());
            task.setProjectCmpy(baseSchedulingDetails.getConstructionUnit());
            task.setProjectPart(baseSchedulingDetails.getStructuralParts());
            task.setWorkMethod(baseSchedulingDetails.getPump());
            task.setCustomerName(baseSchedulingDetails.getOrderNo());
            task.setPlanCount(String.valueOf(baseSchedulingDetails.getPredeterminedQuantity()));
            //获取当前时间，年月日时分秒
            task.setPlanDate(DateUtil.date());
            task.setRecipeNo(recipeNo);
            task.setTaskName(baseSchedulingDetails.getProductName() + baseSchedulingDetails.getOrderNo());
            task.setStationId("1");
            task.setUstate("1");
            task.setOrderId(ParameterCodeEnum.ZERO.getCode());
            task.setItemId(ParameterCodeEnum.ZERO.getCode());
            task.setEmployeeId(ParameterCodeEnum.ZERO.getCode());
            task.setDsl(ParameterCodeEnum.ZERO.getCode());
            task.setZsl(ParameterCodeEnum.ZERO.getCode());
            task.setXsl(ParameterCodeEnum.ZERO.getCode());
            task.setSz(ParameterCodeEnum.ZERO.getCode());
            task.setSz1(ParameterCodeEnum.ZERO.getCode());
            task.setCustomerId(ParameterCodeEnum.ZERO.getCode());
            if (produceLine.contains("1")) {
                //查询任务ID最大值
                Integer maxId = sp1TaskService.getMaxId();
                task.setTaskid(maxId + 1);
                log.info("保存任务单数据:" + task.toString());

                sp1TaskService.save(task);
            } else if (produceLine.contains("2")) {
                Integer maxId = sp2TaskService.getMaxId();
                task.setTaskid(maxId + 1);

                log.info("保存任务单数据:" + task.toString());
                sp2TaskService.save(task);
            }
        }else {
            task = check;
        }
        return task.getTaskid();
    }

    /**
     * 运输单保存
     *
     * @param baseSchedulingDetails
     * @return
     * @throws SQLException
     */
    @ApiOperation(value = "运输单保存-中间件任务", notes = "运输单保存-中间件任务")
    @PostMapping(value = "/transportSave")    //中间件任务
    public R transportSave(@Validated @RequestBody BaseSchedulingDetails baseSchedulingDetails) throws SQLException {
        log.info("运输单保存-中间件任务,baseSchedulingDetails={}", baseSchedulingDetails);
        createTransport(baseSchedulingDetails, baseSchedulingDetails.getTaskId());
        return R.ok();
    }

    //创建运输单
    private Integer createTransport(BaseSchedulingDetails baseSchedulingDetails, Integer taskId) {
        String produceLine = baseSchedulingDetails.getProduceLine();//生产线
        //保存运输单数据
        OutMaster outMaster = new OutMaster();
        outMaster.setProjectCmpy(baseSchedulingDetails.getEngineeringName());
        outMaster.setProjectPart(baseSchedulingDetails.getStructuralParts());
        outMaster.setStationName(baseSchedulingDetails.getProduceLine());
        outMaster.setPlanCount(baseSchedulingDetails.getPredeterminedQuantity());
        outMaster.setTld(baseSchedulingDetails.getSlumps());
        outMaster.setDeliveryName(baseSchedulingDetails.getTransportNo());
        outMaster.setQuantity(baseSchedulingDetails.getProductionQuantity());
        outMaster.setDeliveryDate(DateUtil.date());
        outMaster.setIntensityLevel(baseSchedulingDetails.getProductName());
        outMaster.setOutCount(baseSchedulingDetails.getTrainNumber());
        outMaster.setCementName("PO42.5");
        outMaster.setWorkMethod(baseSchedulingDetails.getIsPump());
        outMaster.setTaskName(baseSchedulingDetails.getProductName() + baseSchedulingDetails.getOrderNo());
        outMaster.setRecipeNo(baseSchedulingDetails.getProductName() + baseSchedulingDetails.getRatioNo());
        outMaster.setCompanyName(baseSchedulingDetails.getOrderNo());
        outMaster.setProjectName(baseSchedulingDetails.getConstructionUnit());
        outMaster.setTruckId(baseSchedulingDetails.getWagon());
        outMaster.setEmployeeId("10");
        outMaster.setCementId(ParameterCodeEnum.ZERO.getCode());
        outMaster.setDistance(ParameterCodeEnum.ZERO.getCode());
        outMaster.setItemId(ParameterCodeEnum.ZERO.getCode());
        outMaster.setOrderId(ParameterCodeEnum.ZERO.getCode());
        //盘次逻辑 生产量/3
        outMaster.setItemNo(getItemNo(baseSchedulingDetails.getProductionQuantity()));
        outMaster.setTotalItemNo(getItemNo(baseSchedulingDetails.getProductionQuantity()));

        outMaster.setTaskID(taskId);
        if (produceLine.contains("1")) {
            Integer maxId = sp1OutMaskService.getMaxId();
            outMaster.setDeliveryID(maxId + 1);
            outMaster.setStationName("1#搅拌站");
            sp1OutMaskService.save(outMaster);
            //包含砂浆的数据额外保存一条
            if(baseSchedulingDetails.getMortar() != null && baseSchedulingDetails.getMortar().compareTo(BigDecimal.ZERO)  > 0) {
                OutMaster shajiangOutMaster = new OutMaster();
                BeanUtil.copyProperties(outMaster, shajiangOutMaster);
                shajiangOutMaster.setDeliveryID(maxId + 2);
                shajiangOutMaster.setRecipeNo(baseSchedulingDetails.getProductName() + "S" + baseSchedulingDetails.getRatioNo());
                shajiangOutMaster.setBSlurry("1");
                shajiangOutMaster.setSlurryQuantity(baseSchedulingDetails.getMortar().toString());
                shajiangOutMaster.setQuantity(baseSchedulingDetails.getMortar());
                shajiangOutMaster.setPlanCount(baseSchedulingDetails.getMortar());

                //盘次逻辑 生产量/3
                shajiangOutMaster.setItemNo(getItemNo(baseSchedulingDetails.getMortar()));
                shajiangOutMaster.setTotalItemNo(getItemNo(baseSchedulingDetails.getMortar()));
                sp1OutMaskService.save(shajiangOutMaster);
            }
        } else if (produceLine.contains("2")) {
            Integer maxId = sp2OutMaskService.getMaxId();
            outMaster.setDeliveryID(maxId + 1);
            outMaster.setStationName("2#搅拌站");
            sp2OutMaskService.save(outMaster);
            //包含砂浆的数据额外保存一条
            if(baseSchedulingDetails.getMortar() != null && baseSchedulingDetails.getMortar().compareTo(BigDecimal.ZERO)  > 0) {
                OutMaster shajiangOutMaster = new OutMaster();
                BeanUtil.copyProperties(outMaster, shajiangOutMaster);
                shajiangOutMaster.setDeliveryID(maxId + 2);
                shajiangOutMaster.setRecipeNo(baseSchedulingDetails.getProductName() + "S" + baseSchedulingDetails.getRatioNo());
                shajiangOutMaster.setBSlurry("1");
                shajiangOutMaster.setSlurryQuantity(baseSchedulingDetails.getMortar().toString());
                shajiangOutMaster.setQuantity(baseSchedulingDetails.getMortar());
                shajiangOutMaster.setPlanCount(baseSchedulingDetails.getMortar());
                //盘次逻辑 生产量/3
                shajiangOutMaster.setItemNo(getItemNo(baseSchedulingDetails.getMortar()));
                shajiangOutMaster.setTotalItemNo(getItemNo(baseSchedulingDetails.getMortar()));
                sp2OutMaskService.save(shajiangOutMaster);
            }
        }
        return outMaster.getDeliveryID();
    }

    /**
     * 盘次计算逻辑 生产量/3 不能整除的情况——> 向上取整
     */
    private Integer getItemNo(BigDecimal productionQuantity) {
        BigDecimal three = new BigDecimal("3");
        BigDecimal remainder = productionQuantity.remainder(three);
        if (remainder.compareTo(BigDecimal.ZERO) == 0) {
            return productionQuantity.divide(three, RoundingMode.DOWN).intValue();
        } else {
            return productionQuantity.divide(three, RoundingMode.DOWN).intValue() + 1;
        }
    }

    /**
     * 生产消耗查询
     *
     * @throws Exception 异常
     */
    @ApiOperation(value = "生产消耗查询", notes = "生产消耗查询")
    @GetMapping(value = "/productionTask")    //中间件任务
    public Result<List<ManufactureDTO>> productionTask(@RequestParam(name = "id1", required = false) String mDate1,
                                                       @RequestParam(name = "id2", required = false) String mDate2) throws Exception {
        log.info("生产消耗查询,id1={},id2={}", mDate1, mDate2);
        //查询生产任务数据
        List<ManufactureDTO> res = new ArrayList<>();
        //查询 SP 1 大于运单id的数据
        if (mDate1 != null) {
            List<ManufactureDTO> sp1 = sp1ManufactureService.selectGtDeliveryId(mDate1);
            if (sp1 != null && sp1.size() > 0) {
                res.addAll(sp1);
            }
        }
        //查询 SP 2 大于运单id的数据
        if (mDate2 != null ) {
            List<ManufactureDTO> sp2 = sp2ManufactureService.selectGtDeliveryId(mDate2);
            if (sp2 != null && sp2.size() > 0) {
                res.addAll(sp2);
            }
        }
        return Result.ok(res);
    }


    /**
     * 生产消耗查询
     *
     * @throws Exception 异常
     */
    @ApiOperation(value = "生产消耗查询", notes = "生产消耗查询")
    @GetMapping(value = "/productionTaskByDate")    //中间件任务
    public Result<List<ManufactureDTO>> productionTaskByDate(@RequestParam(name = "date1", required = false) String mDate1,
                                                       @RequestParam(name = "date2", required = false) String mDate2) throws Exception {
        log.info("生产消耗查询-根据日期,date1={},date2={}", mDate1, mDate2);
        //查询生产任务数据
        List<ManufactureDTO> res = new ArrayList<>();
        //查询 SP 1 大于运单id的数据
        if (mDate1 != null) {
            List<ManufactureDTO> sp1 = sp1ManufactureService.selectByDate(mDate1);
            if (sp1 != null && sp1.size() > 0) {
                res.addAll(sp1);
            }
        }
        //查询 SP 2 大于运单id的数据
        if (mDate2 != null ) {
            List<ManufactureDTO> sp2 = sp2ManufactureService.selectByDate(mDate2);
            if (sp2 != null && sp2.size() > 0) {
                res.addAll(sp2);
            }
        }
        return Result.ok(res);
    }




    /**
     * 出货单消耗查询
     *
     * @throws Exception 异常
     */
    @ApiOperation(value = "出货单任务查询-中间件任务", notes = "出货单任务-中间件任务")
    @GetMapping(value = "/curOutData")    //中间件任务
    public Result<List<CurOutDTO>> curOutData(@RequestParam(name = "line", required = false) String line) throws Exception {

        log.info("出货单任务查询,line={}", line);
        //查询生产任务数据
        List<CurOutDTO> res = new ArrayList<>();
        //查询 SP 1 大于运单id的数据
        if (line != null && line.contains("1")) {
            res = sp1CurOutService.curOutData();
        }
        //查询 SP 2 大于运单id的数据
        if (line != null && line.contains("2")) {
            res = sp2CurOutService.curOutData();
        }
        return Result.ok(res);
    }



}
